package com.example.administrator.mydifferutils;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    private SwipeRefreshLayout srl;
    private SwipeRefreshLayout srl2;
    private RecyclerView rlv;
    private RecyclerView rlv2;
    private List<Bean> list = new ArrayList<>();
    private List<Bean> list2 = new ArrayList<>();
    private MyRecyclerViewAdapter myRecyclerViewAdapter;
    private MyRecyclerViewAdapter myRecyclerViewAdapter2;
    private int tmep = 3;
    private int tmep2 = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //传统的刷新recyclerView数据的方法
        srl = (SwipeRefreshLayout) findViewById(R.id.srl);
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
//                list.set(1, new Bean("平安星", 5));
//                list.add(new Bean("大管家", 6));
//                Bean bean = list.get(2);
//                list.remove(bean);
//                list.add(bean);
                list.clear();
                list.add(new Bean("三" + tmep, 1 + tmep));
                list.add(new Bean("三友" + tmep, 2 + tmep));
                list.add(new Bean("三友智" + tmep, 3 + tmep));
                list.add(new Bean("三友智云" + tmep, 4 + tmep));

                myRecyclerViewAdapter.setList(list);
                myRecyclerViewAdapter.notifyDataSetChanged();

                srl.setRefreshing(false);
                tmep += 3;
            }
        });

        //用DifferUtil刷新recyclerView数据的方法
        srl2 = (SwipeRefreshLayout) findViewById(R.id.srl2);
        srl2.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
//                list2.set(1, new Bean("平安星", 5));
//                list2.add(new Bean("大管家", 6));
//                Bean bean = list2.get(2);
//                list2.remove(bean);
//                list2.add(bean);
                list2.clear();
                list2.add(new Bean("三"+ tmep2, 1+ tmep2));
                list2.add(new Bean("三友"+ tmep2, 2+ tmep2));
                list2.add(new Bean("三友智"+ tmep2, 3+ tmep2));
                list2.add(new Bean("三友智云"+ tmep2, 4+ tmep2));

                DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new MyDifferUtilCallback(myRecyclerViewAdapter2.getList(), list2));
                myRecyclerViewAdapter2.setList(list2);
                diffResult.dispatchUpdatesTo(myRecyclerViewAdapter2);

                srl2.setRefreshing(false);
                tmep2 += 3;
            }
        });
        srl.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        srl2.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        rlv = (RecyclerView) findViewById(R.id.rlv);
        rlv2 = (RecyclerView) findViewById(R.id.rlv2);
        initData();
    }

    private void initData() {

        list.add(new Bean("三"+0, 1));
        list.add(new Bean("三友"+0, 2));
        list.add(new Bean("三友智"+0, 3));
        list.add(new Bean("三友智云"+0, 4));
        myRecyclerViewAdapter = new MyRecyclerViewAdapter(list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rlv.setLayoutManager(linearLayoutManager);
        rlv.setAdapter(myRecyclerViewAdapter);


        list2.add(new Bean("三"+0, 1));
        list2.add(new Bean("三友"+0, 2));
        list2.add(new Bean("三友智"+0, 3));
        list2.add(new Bean("三友智云"+0, 4));
        myRecyclerViewAdapter2 = new MyRecyclerViewAdapter(list2);
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rlv2.setLayoutManager(linearLayoutManager2);
        rlv2.setAdapter(myRecyclerViewAdapter2);
    }

}
